package com.platform.oss.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.oss.entity.Oss;
import com.platform.oss.mapper.OssMapper;
import com.platform.oss.service.OssService;
import com.platform.oss.utils.ConstantProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Transactional
@Service
public class OssServiceImpl extends ServiceImpl<OssMapper, Oss> implements OssService {
    //上传文件
    @Override
    public String uploadFileAvatar(MultipartFile file) {
        try {
            //获取阿里云存储相关常量
            String endPoint = ConstantProperties.END_POIND;
            String accessKeyId = ConstantProperties.ACCESS_KEY_ID;
            String accessKeySecret = ConstantProperties.ACCESS_KEY_SECRET;
            String bucketName = ConstantProperties.BUCKET_NAME;
            //上传文件到oss
            //创建OSS实例
            OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
            //获取上传文件流
            InputStream inputStream = file.getInputStream();
            //获取文件名称
            String fileName = file.getOriginalFilename();
            //调用oss方法实现上传
            //第一个参数 Bucker名称
            //第二个参数 上传到oss文件路径和文件名称 aa//bb//a.png
            //第三个参数 上传文件流
            PutObjectResult Result = ossClient.putObject(bucketName, fileName, inputStream);
            System.out.println("###############################");
            String VersionId = Result.getVersionId();
            // 关闭OSSClient。
            ossClient.shutdown();
            return VersionId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //永久删除文件
    @Override
    public void DeleteFile(String filename) {
        //获取阿里云存储相关常量
        String endPoint = ConstantProperties.END_POIND;
        String accessKeyId = ConstantProperties.ACCESS_KEY_ID;
        String accessKeySecret = ConstantProperties.ACCESS_KEY_SECRET;
        String bucketName = ConstantProperties.BUCKET_NAME;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

        try {
            // 删除指定版本的Object。
            ossClient.deleteObject(bucketName, filename);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }



    //根据文件名下载
    @Override
    public void downloadfile(OutputStream os, String filename) {
        //获取阿里云存储相关常量
        String endPoint = ConstantProperties.END_POIND;
        String accessKeyId = ConstantProperties.ACCESS_KEY_ID;
        String accessKeySecret = ConstantProperties.ACCESS_KEY_SECRET;
        String bucketName = ConstantProperties.BUCKET_NAME;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        OSSObject ossObject = ossClient.getObject(bucketName, filename);
        // 读取文件内容。
        BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
        BufferedOutputStream out = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int lenght = 0;
        try {
            while ((lenght = in.read(buffer)) != -1) {
                out.write(buffer, 0, lenght);
            }
            if (out != null) {
                out.flush();
                out.close();
            }
            if (in != null) {
                in.close();
            }
        }catch(Exception e) {
           e.printStackTrace();
        }
    }
}











